<?php

/**
 * Project: Espolon
 * Licence: BSD License
 * Link: http://espolon.org/
 * Copyright: Nicolas de Bari Embriz Garcia Rojas <nbari at espolon dot org>
 * Author: Nicolas de Bari Embriz Garcia Rojas <nbari at espolon dot org>
 * Version: Beta
 */

if (!defined('SITE_ROOT')) exit('No direct script access allowed');

class Section extends Dispatcher {
  private $id;
  private $name;
  private $alias;
  private $desc;
  private $level;
  private $root_id;
  private $left;
  private $right;
  private $msg;

  function setId ($id) { $this->id = (is_number($id)) ? $id : sign_out(); }
  function setName ($name) { $this->name = $name; }
  function setAlias ($alias) { $this->alias = $alias; }
  function setDesc ($desc) { $this->desc = $desc; }
  function setLevel ($level) { $this->level = $desc; }
  function setMsg ($msg) {$this->msg = $msg; }

  function getId() { return $this->id; }
  function getName() { return $this->name; }
  function getAlias() {return $this->alias; }
  function getDesc() { return $this->desc; }
  function getLevel() { return $this->level; }
  function getRoot_id() { return $this->root_id; }
  function getMsg() { return $this->msg; }

  function checkExists ($op, $value=null) {
    switch($op) {
      case 'section':
        $alias = explode(' ',$this->getAlias());
        $alias = array_merge($alias, array($this->getName()));
        if (array_search_dups($alias)) {
         return false;
        } else {
          $keywords = $alias;
          $rs = $this->getDb()->GetCol("SELECT TRIM(CONCAT(name,' ', alias)) FROM mod_sections WHERE level=0");
          foreach ($rs as $value) {
            $row = explode(' ', $value);
            $keywords = array_merge($row, $keywords);
          }
          return (array_search_dups($keywords)) ? false : true;
        }
      break;
      case 'modify':
        $alias = explode(' ',$this->getAlias());
        $alias = array_merge($alias, array($this->getName()));
        if (array_search_dups($alias)) {
         return false;
        } else {
          $keywords = $alias;
          $rs = $this->getDb()->GetCol("SELECT TRIM(CONCAT(name,' ', alias)) FROM mod_sections WHERE root_id=$this->root_id AND level=$this->level AND id !=".$this->getId()."");
          foreach ($rs as $value) {
            $row = explode(' ', $value);
            $keywords = array_merge($row, $keywords);
          }
          return (array_search_dups($keywords)) ? false : true;
        }
      break;
      case 'node':
        $alias = explode(' ',$this->getAlias());
        $alias = array_merge($alias, array($this->getName()));
        if (array_search_dups($alias)) {
         return false;
        } else {
          $keywords = $alias;
          $level = $this->level + 1;
          $rs = $this->getDb()->GetCol("SELECT TRIM(CONCAT(name,' ', alias)) FROM mod_sections WHERE root_id=$this->root_id AND level=$level");
          foreach ($rs as $value) {
            $row = explode(' ', $value);
            $keywords = array_merge($row, $keywords);
          }
          return (array_search_dups($keywords)) ? false : true;
        }
      break;
    }
  }

  function addSection($details) {
    $sec['name'] = $details['section'];
    $sec['alias'] = $details['alias'];
    $sec['description'] = $details['description'];
    $sec['root_id'] = $this->getDb()->GetOne("SELECT MAX(root_id) FROM mod_sections") + 1;
    $sec['lft'] = 1;
    $sec['rgt'] = 2;
    $sec['level'] = 0;
    $rs = $this->getDb()->AutoExecute('mod_sections', $sec, 'INSERT');
  }

  function addNode($details) {
    $newLeft = $this->right;
    $newRight = $newLeft + 1;
    $this->getDb()->Execute("UPDATE mod_sections SET lft = lft + 2 WHERE lft >= $newLeft AND root_id = ".$this->root_id."");
    $this->getDb()->Execute("UPDATE mod_sections SET rgt = rgt + 2 WHERE rgt >= $newLeft AND root_id = ".$this->root_id."");

    $node['name']        = $details['node'];
    $node['alias']       = $details['alias'];
    $node['description'] = $details['description'];
    $node['root_id']     = $this->root_id;
    $node['lft']         = $newLeft;
    $node['rgt']         = $newRight;
    $node['level']       = $this->level + 1;

    $rs = $this->getDb()->AutoExecute('mod_sections', $node, 'INSERT');
  }

  function modify ($details) {
    $data['name']        = $details['node'];
    $data['alias']       = $details['alias'];
    $data['description'] = $details['description'];
    $rs = $this->getDb()->AutoExecute('mod_sections', $data, 'UPDATE', 'id='.$this->getId());
  }

  function delete() {
    if ($this->isLeaf($this->getId())) {
      $rs = $this->getDb()->GetRow("SELECT * FROM mod_sections WHERE id=".$this->getId()."");
      $lft = $rs['lft'];
      $rgt = $rs['rgt'];
      $width =  $rgt - $lft + 1;

      $this->getDb()->Execute("DELETE FROM mod_sections WHERE id=".$this->getId()."");
      $this->getDb()->Execute("UPDATE mod_sections SET rgt = rgt - $width WHERE rgt >= $rgt AND root_id = $rs[root_id]");
      $this->getDb()->Execute("UPDATE mod_sections SET lft = lft - $width WHERE lft >= $rgt AND root_id = $rs[root_id]");
      return true;
    } else {
      return false;
    }
  }

  function getSections ($id=null) {
   return (isset($id)) ? $this->getDb()->GetRow("SELECT * FROM mod_sections WHERE id=$id") : $this->getDb()->GetAll("SELECT * FROM mod_sections WHERE level=0 ORDER BY id");
  }

  function getTree($id) {
    if (!is_number($id)) {
      $id = $this->getDb()->GetOne("SELECT id FROM mod_sections WHERE level=0 AND name='$id'");
    }
    $tree = $this->getDb()->GetRow("SELECT * FROM mod_sections WHERE id=$id");
    $this->id      = $tree['id'];
    $this->root_id = $tree['root_id'];
    $this->left    = $tree['lft'];
    $this->right   = $tree['rgt'];
    $this->level   = $tree['level'];
  }

  function display_tree($root_id=null) {
    if ($root_id) {
      $min = ($rs = $this->getDb()->GetOne("SELECT MIN(level) FROM mod_sections WHERE lft > 0 AND root_id=$root_id AND lft BETWEEN $this->left AND $this->right AND level > $this->level ORDER BY root_id, lft ASC")) ? $rs : 0;
      $tree = $this->getDb()->GetAll("SELECT id, name, alias, description, (level - $min) as level FROM mod_sections WHERE lft > 0 AND root_id=$root_id AND lft BETWEEN $this->left AND $this->right AND level > $this->level ORDER BY root_id, lft ASC");
    } else {
      $tree = $this->getDb()->GetAll("SELECT * FROM mod_sections WHERE lft > 0 ORDER BY root_id, lft ASC");
    }
    return $tree;
  }

  function getPath() {
    $path = $this->getDb()->GetCol("SELECT parent.name FROM mod_sections AS node, mod_sections AS parent WHERE node.root_id=$this->root_id AND parent.root_id=$this->root_id AND node.lft BETWEEN parent.lft AND parent.rgt AND node.id=$this->id ORDER BY node.lft");
    return $path;
  }

  function isLeaf() {
    return ($this->getDb()->GetOne("SELECT rgt - lft FROM mod_sections WHERE id=".$this->getId()."") == 1) ? true : false;
  }

} //end of class

?>